let time = require('../../utils/util.js');
let app = getApp();
Page({
  data: {
    winHeight: 0,
    currentTab: 0,
    openid: '',
    iconFlag: false,
    salesmanName: '',
    supervisorName:'',
    bumenmingcheng:'',
    startTime: '',
    endTime: '',
    subFlag:'yes',
    postFlag:false,
    navData: [{
      name: "首页",
      current: 0,
      style: 0,
      ico: 'icon-deng',
      fn: 'navJumpPage',
      id: '/pages/home/home'
    }, {
      name: "客户",
      current: 0,
      style: 0,
      ico: 'icon-guanlikehu',
      fn: 'navJumpPage',
      id: '/pages/client/client'
    }, {
      name: "添加汇报",
      current: 0,
      style: 1,
      ico: '',
      fn: 'navJumpPage',
      id: '/pages/addReport/addReport'
    }, {
      name: "签到",
      current: 1,
      style: 0,
      ico: 'icon-xiebaogao',
      fn: 'navJumpPage',
      id: ''
    }, {
      name: "我的",
      current: 0,
      style: 0,
      ico: 'icon-dkw_wode',
      fn: 'navJumpPage',
      id: '/pages/salesmanMine/salesmanMine'
    }],
    userInfo: [],
    subList:[],
  },
  bindChange: function(e) {
    this.setData({
      currentTab: e.detail.current
    });
    e.detail.current == '0' ? this.loadSignList() : this.subInfoList();
  },
  swichNav(e) {
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  onLoad() {
    let that = this,
      newDate = new Date(),
      startTime = time.formatTime(newDate); //获取时间戳
    that.setData({
      startTime: that.Month(),
      endTime: startTime
    });

    //获取openid
    wx.getStorage({
      key: 'openid',
      success(res) {
        that.setData({
          openid: res.data
        });
        //加载列表
        that.loadSignList();
        if (that.data.subFlag === 'yes') {
          that.subInfoList();
        }
      },
    });


    //判断是否为部门主管
    wx.getStorage({
      key: 'post',
      success(res){
        let postFlag = false;
        res.data === '部门主管' ? postFlag = true : postFlag = false;
        that.setData({
          postFlag: postFlag
        });
      }
    });
    //获取姓名信息
    wx.getStorage({
      key: 'supervisorName',
      success(res) {
        that.setData({
          supervisorName: res.data
        });
      }, fail(res) {
        that.setData({
          supervisorName: ''
        });
      }
    });

    //获取职位信息
    wx.getStorage({
      key: 'bumenmingcheng',
      success(res) {
        console.log(res) 
        that.setData({
          bumenmingcheng: res.data
        });
      }
    });
    //获取高度
    wx.getSystemInfo({
      success(res) {
        that.setData({
          winHeight: res.windowHeight
        })
      }
    });   
  },
  loadSignList() {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this,
      startTime = that.data.startTime,
      endTime = that.data.endTime;
    app.get('GetKaoQin', {
      action: 'getGeRenKaoQin',
      openid: that.data.openid,
      dtstart: startTime, //开始时间
      dtend: endTime, //结束时间
    }).then((res) => {
      wx.hideLoading();
      that.setData({
        userInfo: res
      });
      if (res.length === 0) {
        app.toast('未查找到数据')
      }
    });
  },
  subInfoList(){
    wx.showLoading({
      title: '加载中...',
    });
    let that = this,
      startTime = that.data.startTime,
      endTime = that.data.endTime;
    app.get('GetKaoQin', {
      action: 'getXiaJiKaoQin',
      openid: that.data.openid,
      BuMenMingCheng: that.data.bumenmingcheng,
      YeWuYuanXingMing: that.data.salesmanName,
      ZhuGuanXingMing: that.data.supervisorName,
      dtstart: startTime, //开始时间
      dtend: endTime, //结束时间
    }).then((res) => {
      wx.hideLoading();
      that.setData({
        subList: res
      })
    });
  },
  //跳转详情
  signDetail(e) {
    let resData = e.currentTarget.dataset,
      str = '?name=' + resData.name + '&comment=' + resData.comment + '&address=' + resData.address + '&url=' + resData.url + '&time=' + resData.time;
    wx.navigateTo({
      url: '/pages/signInDetail/signInDetail' + str
    });
  },
  startDate(e) {
    this.setData({
      startTime: e.detail.value
    })
  },
  endDate(e) {
    this.setData({
      endTime: e.detail.value
    });
  },
  salesmanName(e) {
    this.setData({
      salesmanName: e.detail.value
    });
  },
  inquireBtn() {
    let flag = '';
    flag = !this.data.iconFlag;
    this.setData({
      iconFlag: flag
    })
  },
  formReset() {
    this.setData({
      startTime: '',
      endTime: ''
    });
    app.toast('搜索条件已重置');
  },
  formSubmit(e) {
    let formData = e.detail.value,
      startTime = formData.startTime,
      endTime = formData.endTime;
    if (!startTime) {
      app.toast('请选择起始时间');
    } else if (!endTime) {
      app.toast('请选择结束时间');
    } else {  
      this.setData({
        startTime: startTime,
        endTime: endTime,
      });   
      if (this.data.currentTab == '0'){
        this.loadSignList();
      }else{
        this.subInfoList();
        this.setData({
          salesmanName: formData.name
        });
      }
    }
  },
  //获取当前月的后2两个月
  Month(month) {
    let date = new Date();
    var year = date.getFullYear(); //年
    var month = date.getMonth() - 1; //月 1个月 month从0开始，所以要加1
    if (month > 12) {
      year++;
      month -= 12;
    }
    if (month < 10) {
      month = "0" + month;
    }
    var date2 = new Date(year, month, 0); //新的年月
    var day1 = date.getDate();
    var day2 = date2.getDate();
    if (day1 > day2) { //防止+6月后没有31天
      day1 = day2;
    }
    let str = year + '-' +
      month + '-' +
      day1;
    console.log(str)
    return str;
  }
})